﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using 立库托盘入库.bean;
using 立库托盘入库.ViewModel;

namespace 立库托盘入库
{
    /// <summary>
    /// SettinfScan.xaml 的交互逻辑
    /// </summary>
    public partial class SettinfScan : UserControl
    {
        MainViewModel _viewModel;
        private string path;
        public SettinfScan()
        {
            InitializeComponent();
            path = AppDomain.CurrentDomain.BaseDirectory;
            _viewModel = MainViewModel.MainViewModelCreate();
            base.DataContext = _viewModel;
            init();
        }
        private void Scan_quxiao(object sender, RoutedEventArgs e)
        {
            _viewModel.IsScanDialogOpen = Visibility.Hidden;
        }
        private void Scan_queding(object sender, RoutedEventArgs e)
        {
            _viewModel.IsScanDialogOpen = Visibility.Hidden;
            writScanData();

        }

        private void closedialog(object sender, MouseButtonEventArgs e)
        {
            _viewModel.IsScanDialogOpen = Visibility.Hidden;
        }

        public void init()
        {
            _viewModel.ChuanKous = SerialPort.GetPortNames();
            _viewModel.ChuankouSettingBean = new ChuankouSettingBean();
            readXChuanKou();
        }

        //保存串口设置数据
        public void writScanData()
        {
            string filePath = path + "save\\ChuanKou.xml";
            XElement datas = new XElement("Data");
            XElement data = new XElement("ChuanKouBean");
            data.Add(new XElement("PortName", _viewModel.PortName));
            data.Add(new XElement("BaudRate", _viewModel.BaudRate));
            data.Add(new XElement("Parity", _viewModel.Parity));
            data.Add(new XElement("DataBits", _viewModel.DataBits));
            data.Add(new XElement("StopBits", _viewModel.StopBits));
            datas.Add(data);
            datas.Save(filePath);
            _viewModel.sxcom();
        }

        public void readXChuanKou()
        {
            try
            {
                if (Directory.Exists(path + "save") == false)//如果不存在就创建file文件夹
                {
                    Directory.CreateDirectory(path + "save");
                }
                if (File.Exists(path + "save\\ChuanKou.xml"))
                {
                    string filePath = path + "save\\ChuanKou.xml";
                    XElement xmlDoc = XElement.Load(filePath);
                    //XElement d = xmlDoc.Element("UserData");
                    var data = xmlDoc.Descendants("ChuanKouBean");
                    foreach (var item in data)
                    {
                        _viewModel.PortName = item.Element("PortName").Value;
                        _viewModel.BaudRate = int.Parse(item.Element("BaudRate").Value);
                        _viewModel.Parity = item.Element("Parity").Value;
                        _viewModel.DataBits = int.Parse(item.Element("DataBits").Value);
                        _viewModel.StopBits = item.Element("StopBits").Value;
                    }
                }
                else
                {
                    //不存在文件
                    //Directory.CreateDirectory(path + "save\\Login.xml");//创建该文件

                    string filePathOne = System.IO.Path.Combine(path + "save", "ChuanKou.xml");
                    File.Create(filePathOne);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("sssssssssssss" + e.ToString());
            }

        }


    }
}
